package gen.tools.jenkins;

import gen.tools.Widgets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JobExecutionFail extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public JobExecutionFail() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		/*	執行步驟
		 * 
		 * 1. 寄發通知信
		 * 2. 將該此Job的Trigger回復成預設值
		 * 
		 * */
		
		
		/*
		 * BE失敗後，重新執行的步驟
		 * 1. 修改Job的 postBuild Step
		 * 2. 修改繳交記錄狀態
		 * 3. 新增BE執行記錄
		 * 
		 * */ 
		
		final Widgets wid = new Widgets();
		response.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/text");
		
		 
		final int chkInId = wid.getParNumberFromRequest( request.getParameter("chkInId") ); 
		final String subsysId = request.getParameter("subsysId");
		final String performerId = request.getParameter("performerId");
		final String currentJobName = request.getParameter("jobName");					//目前出事的Job
		final String upstreamJobName = request.getParameter("upstreamJobName");			//上游Trigger的job
		final String rootJobName = request.getParameter("rootJobName");					//此次的根Job
		final int currentJobBuildNumber = wid.getLastBuildNumber(currentJobName);
		final int currentJobNumber = wid.getLastBuildNumber( currentJobName );
		final boolean isDailyBuild = "MR".equalsIgnoreCase( currentJobName.substring(0,3) ); 
		
		
		System.out.println("目前執行失敗的Job是："+currentJobName+",它的job執行序號是："+currentJobBuildNumber);
		
		int upstreamJobSerialNumber = 0; 
		int rootJobBuildNumber = 0;
		
		if( currentJobName.equalsIgnoreCase(rootJobName) ) {
			upstreamJobSerialNumber = currentJobBuildNumber;
			rootJobBuildNumber = currentJobBuildNumber;
		} else {
			upstreamJobSerialNumber = 0;wid.getParNumberFromRequest( request.getParameter("upJobBuildNumber") );
			rootJobBuildNumber = wid.getParNumberFromRequest( request.getParameter("rootJobBuildNumber") );
		}
		
		
		int chkInStatusId = 8;	//jenkins 執行Build版封裝失敗
		
		try {
			//先將SVN上的版次值回覆
			wid.recoverJenkinsJobSVNver(currentJobName);
			System.out.println("回復Jenkins上的SVN執行版次成功");
			
			wid.updateCHKINstatus(chkInId, chkInStatusId);
			wid.createBElog(currentJobName, currentJobBuildNumber,upstreamJobName,upstreamJobSerialNumber,rootJobName,rootJobBuildNumber, chkInId, performerId, isDailyBuild);
			//寄發通知信
			//收件名單
			ArrayList<String[]> mailList = wid.getMailList(subsysId, 7);	//CM人員
			mailList.add( wid.getEmailAddr(performerId) );	//繳交人員
			//信件抬頭
			String mailSubjectText = "jenkins封裝發生錯誤，"+currentJobName+","+currentJobNumber;
			//信件本文
			String mailBodyText = wid.JENKINSURL+"job/"+currentJobName+"/"+currentJobNumber;
			//寄發
			wid.sendMailMain(mailList, mailSubjectText, mailBodyText);
			
			
			//將該此Job的Trigger回復成預設值
			wid.resetJobPostBuildURL(currentJobName);
			
			
			
		} catch (Exception e) {
			wid.errLog(e);
			e.printStackTrace();
		}
		
		String res = "觸發失敗通知";
		response.getWriter().write(res);
	}

}
